package rmi;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;

/**
 * class to locate our most "secure" policy file
 *
 * @author srasul
 *
 */
public class PolicyFileLocator {
    public static final String SERVER_POLICY_FILE_NAME = "/server.policy";
    public static final String CLIENT_POLICY_FILE_NAME = "/client.policy";

    public static String getLocationOfPolicyFile(boolean isServer) {
        try {
            File tempFile = File.createTempFile("rmi-base", ".policy");
            InputStream is;
            if(isServer) {
            	is = PolicyFileLocator.class.getResourceAsStream(SERVER_POLICY_FILE_NAME);
            }
            else {
            	is = PolicyFileLocator.class.getResourceAsStream(CLIENT_POLICY_FILE_NAME);
            }
            BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile));
            int read = 0;
            while((read = is.read()) != -1) {
                writer.write(read);
            }
            writer.close();
            tempFile.deleteOnExit();
            return tempFile.getAbsolutePath();
        }
        catch(IOException e) {
            throw new RuntimeException(e);
        }
    }
}
